package com.edu.gdkm.servlet.film;

import com.edu.gdkm.db.table.film;
import com.edu.gdkm.dao.dypDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/filmServlet")
public class filmServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        film film = new film();     // 电影对象
        String id = req.getParameter("id"); // 获取电影 ID
        try {
            // 数据库执行
            dypDao db = new dypDao();
            ResultSet result = db.select("call sel_film(" + id + ")");
            // 封装表数据
            if (result.next()){
                film.setName(result.getString("fname"));
                film.setPrice(result.getString("fprice"));
                film.setClassify(result.getString("fclassify"));
                film.setTime(result.getString("ftime"));
                film.setId(id);
            }
            db.close(result);
        } catch (SQLException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        // 存入会话域
        HttpSession session = req.getSession();
        session.setAttribute("film", film);
        // 转发 => 单个电影详情页
        req.getRequestDispatcher("film.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
